
"ui";

var storage = storages.create("main")

var 看看 = "https://gitee.com/fridayon/test/raw/main/lookscan.js"  //刷新缓存较快
var 看小说2h = "https://gitee.com/fridayon/test/raw/main/read.js"
var 看小说15min = "https://gitee.com/fridayon/test/raw/main/read(15min).js"

//专用模块
var 叮叮充电 = "https://gitee.com/fridayon/test/raw/main/ddcd.js"
var 快成 = "https://gitee.com/fridayon/test/raw/main/kc.js"
var 搜狐 = "https://gitee.com/fridayon/test/raw/main/souhu.js"


var list = ""
var model = device.model // 获取手机型号
console.info("手机型号：" + model)
storage.put("model",model)
var width = device.width
var color = "#009688";

threads.start(function(){
    xuanfu();
});
threads.start(function(){
    sleep(4000)
    back()
});
ui.layout(
    <drawer id="drawer">
        <vertical>
            <appbar>
                <toolbar id="toolbar" title="云端脚本"/>
                <tabs id="tabs"/>
            </appbar>
            <viewpager id="viewpager">
                <frame>
                    <scroll>
                    <vertical>
                        <vertical margin="16">
                    <horizontal><text>token</text>
                        <input id="token" padding="20" width = {width} text = "请输入token"></input>
                    </horizontal>
                    </vertical>
                        <text textSize="18sp" padding="8" bg="#FF00FF" margin = "5" gravity="center" alpha="0.5" textColor="white">通用模块</text>
                        <horizontal padding="16">
                            <checkbox id="look" text="看看"/>
                            <checkbox id="readbook1" text="看小说(通用)"/>
                            <checkbox id="readbook2" text="看小说(15min)"/>
                        </horizontal>
                        <text textSize="18sp" padding="8" bg="#FF00FF" margin = "5" gravity="center" alpha="0.5" textColor="white">专用模块</text>
                        <horizontal padding="16">
                            <checkbox id="ddcd" text="叮叮充电"/>
                            <checkbox id="kc" text="快成"/>
                            <checkbox id="sh" text="搜狐视频"/>
                        </horizontal>    
                        <horizontal padding="16">
                            
                        </horizontal>    
                        <horizontal padding="16">
                            
                        </horizontal>  
                        <horizontal padding="16">                   
                        </horizontal> 
                        <button id="ok" text="开始"/> 
                    </vertical>
                    </scroll>

                </frame>
                <frame>
                    <vertical>
                        <text textSize="16sp" padding="8" bg="#FF00FF" margin = "5" alpha="0.5" textColor="white">通用脚本说明：</text>
                        <text textSize="14sp" padding="8">1.看看：设备通用，模拟上滑、返回、等待</text>
                        <text textSize="13sp" padding="18" textColor="black">适用云扫码、10秒阅读、番茄看看</text>
                        <text textSize="14sp" padding="8">2.看小说（通用）：设备通用，模拟向左滑动翻页</text>
                        <text textSize="13sp" padding="18" textColor="black">适用阅读小说app</text>
                        <text textSize="14sp" padding="8">3.看小说（15min）：设备通用，模拟点击右上角，阅读15分钟</text>
                        <text textSize="13sp" padding="18" textColor="black">适用短时间阅读</text>
                        <text textSize="16sp" padding="8" bg="#FF00FF" margin = "5" alpha="0.5" textColor="white">专用脚本说明：</text>
                        <text textSize="14sp" padding="8">针对特定app进行模拟点击任务</text>
                        <text textSize="14sp" padding="8">1.快手极速版：进行每日福利视频、看直播得金币任务</text>
                        <text textSize="13sp" padding="18" textColor="black">建议自己先手动做完签到任务和看一次福利视频再进行</text>
                    </vertical>
                    
                    
                </frame>
            </viewpager>
        </vertical>
        <vertical layout_gravity="left" bg="#ffffff" w="280">
            <img w="280" h="200" scaleType="fitXY" src="http://images.shejidaren.com/wp-content/uploads/2014/10/023746fki.jpg"/>
            <list id="menu">
                <horizontal bg="?selectableItemBackground" w="*">
                    <img w="50" h="50" padding="16" src="{{this.icon}}" tint="{{color}}"/>
                    <text textColor="black" textSize="15sp" text="{{this.title}}" layout_gravity="center"/>
                </horizontal>
            </list>
        </vertical>
    </drawer>
);

设置选择()
//创建选项菜单(右上角)
ui.emitter.on("create_options_menu", menu=>{
    menu.add("设置");
    menu.add("关于");
});
//监听选项菜单点击
ui.emitter.on("options_item_selected", (e, item)=>{
    switch(item.getTitle()){
        case "设置":
            toast("还没有设置");
            break;
        case "关于":
            alert("关于", "云端脚本v1.0");
            break;
    }
    e.consumed = true;
});
activity.setSupportActionBar(ui.toolbar);

//设置滑动页面的标题
ui.viewpager.setTitles(["脚本", "脚本说明"]);
//让滑动页面和标签栏联动
ui.tabs.setupWithViewPager(ui.viewpager);

//让工具栏左上角可以打开侧拉菜单
ui.toolbar.setupWithDrawer(ui.drawer);

ui.menu.setDataSource([
  {
      title: "退出",
      icon: "@drawable/ic_exit_to_app_black_48dp"
  }
]);

ui.menu.on("item_click", item => {
    switch(item.title){
        case "退出":
            ui.finish();
            break;
    }
})

ui.ok.click(function() {
    var storage = storages.create("main")
    main()
    存储选择()
})
setTimeout(function(){
    设置选择()
},3000)
function main() {
    if(ui.look.checked) {
        toast("开始，看看")
        var e = ""
        threads.start(function() {
            引入脚本(看看)
            e = engines.execScript('运行看看', list)
            engines.myEngine("main.js").forceStop() 
            引入脚本("https://gitee.com/fridayon/test/raw/main/close.js")
            toastLog("广告监控已经开启。。")
            sleep(1500)
            engines.execScript('运行广告监控', list)
        }) 
    }
    if(ui.readbook1.checked) {
        toast("开始，阅读（通用）")
        threads.start(function() {
            引入脚本("https://gitee.com/fridayon/test/raw/main/close.js")
            toastLog("广告监控已经开启。。")
            sleep(1500)
            engines.execScript('运行广告监控', list)
            引入脚本(看小说2h)
            engines.execScript('运行看小说(通用)', list)
            engines.myEngine("main.js").forceStop() 
        })  //开子线程
    }
    if(ui.readbook2.checked) {
        toast("开始，阅读（15min）")
        threads.start(function() {
            引入脚本("https://gitee.com/fridayon/test/raw/main/close.js")
            toastLog("广告监控已经开启。。")
            sleep(1500)
            engines.execScript('运行广告监控', list)
            引入脚本(看小说15min)
            engines.execScript('运行看小说15min', list)
            engines.myEngine("main.js").forceStop()
        })  //开子线程
    }
    if(ui.ddcd.checked) {
        toast("开始，叮叮充电")
        threads.start(function() {
            引入脚本(叮叮充电)
            engines.execScript('运行叮叮充电', list)
            threads.start(function(){
                xuanfu()
            })
            engines.myEngine("main.js").forceStop() 
        })  //开子线程
    }
    if(ui.kc.checked) {
        toast("开始，快成")
        threads.start(function() {
            引入脚本(快成)
            engines.execScript('运行快成', list)
            threads.start(function(){
                xuanfu()
            })
            engines.myEngine("main.js").forceStop() 
        })  //开子线程
    }
    if(ui.sh.checked) {
        toast("开始，搜狐视频")
        threads.start(function() {
            引入脚本(搜狐)
            engines.execScript('运行搜狐', list)
            threads.start(function(){
                xuanfu()
            })
            engines.myEngine("main.js").forceStop() 
        })  //开子线程
    }
    
}



function 引入脚本(url) {
    var res = http.get(url)
    list = res.body.string() //脚本文字
    return list
}

function 存储选择(){
    storage.put("token",ui.token.text())
    storage.put("ddcd",ui.ddcd.checked)  //叮叮充电   
    storage.put("kc",ui.kc.checked)  //快成  
}
function 设置选择(){
    if(storage.get("ddcd") != null && storage.get("kc") != null ){
        ui.ddcd.checked = storage.get("ddcd")
        ui.kc.checked = storage.get("kc")              
    }
    if(storage.get("token") != null){
        setText(0,storage.get("token"))
    }
}


//悬浮窗
    //=====================================================
    function xuanfu() {
        var window = floaty.window(
            <frame padding="1">
                <vertical  >
                    <horizontal>
                        <button id="action" text="点击" w="35" h="50" style="Widget.AppCompat.Button.Colored" textSize="12sp" />
                        <button id="begin" text="日志" w="35" h="50" textSize="12sp" />
                        <button id="end" text="结束" w="35" h="50" textSize="12sp" />
                        <button id="times" text="计时" w="35" h="50" textSize="12sp" />
                    </horizontal>
    
                    <com.stardust.autojs.core.console.ConsoleView h="150" w="180" id="console" bg="#80000000" />
    
    
                </vertical>
            </frame>
    
        );
    
        window.console.setConsole(runtime.console);
        window.setPosition(0, device.height / 5 * 3);
        // window.console.findViewById(org.autojs.autojs.R.id.input_container).setVisibility(android.view.View.GONE);
        window.console.attr("visibility", "gone");
        
        //开始计时
        var before = new Date();
    
        //日志和结束按钮不显示
        window.begin.setVisibility(8)
        window.end.setVisibility(8)
        window.times.setVisibility(8)
        // window.console.setTouchable(false);
    
        var x = 0, y = 0;
        var windowX, windowY;
        var downTime;
        window.action.setOnTouchListener(function (view, event) {
            switch (event.getAction()) {
                case event.ACTION_DOWN:
                    x = event.getRawX();
                    y = event.getRawY();
                    windowX = window.getX();
                    windowY = window.getY();
                    downTime = new Date().getTime();
                    return true;
                case event.ACTION_MOVE:
                    //移动手指时调整悬浮窗位置
                    window.setPosition(windowX + (event.getRawX() - x),
                        windowY + (event.getRawY() - y));
                    return true;
                case event.ACTION_UP:
                    //手指弹起时如果偏移很小则判断为点击
                    if (Math.abs(event.getRawY() - y) < 5 && Math.abs(event.getRawX() - x) < 5) {
                        onClick();
                    }
                    return true;
            }
            return true;
        });
    
        function onClick(str, sec) {
    
            if (window.end.visibility == 8) {
                window.console.setVisibility(0)
                window.end.setVisibility(0)
                window.times.setVisibility(0)
            } else {
                window.console.setVisibility(8)
                window.end.setVisibility(8)
                window.times.setVisibility(8)
            }
        }
    
        var timerId;
    
        function 显示状态信息(str, sec) {
            try {
                clearTimeout(timerId);
            }
            catch (error) {
                toastLog("error");
            }
            ui.run(() => {
                window.action.setText(str);
            });
            timerId = setTimeout(function () {
                ui.run(() => {
                    window.action.setText("");
                });
            }, sec * 1000);
        }
    
    
        window.begin.click(() => {
            window.begin.text() == "显示日志"
            if (window.begin.text() == "显示日志") {
                window.begin.setText("隐藏日志")
                window.console.attr("visibility", "visible");
            } else {
                window.begin.setText("显示日志")
                window.console.attr("visibility", "gone");
            }
        });
    
        window.end.click(() => {
            floaty.closeAll();
            engines.stopAll();
            exit();
        });
        window.times.click(() => {
            var now =new Date();
            var last = Math.floor((now - before) / 1000)
            if(last >= 60) {
                var minutes =  Math.floor(last / 60)
            } else {
                var minutes = 0
            }
            var seconds = last - minutes * 60
            if(minutes > 0 && seconds < 10){
                console.log("运行了：" + minutes + "分 " + "0" + seconds + "秒")
            } 
            if(minutes > 0 && seconds > 10){
                console.log("运行了：" + minutes + "分 "  + seconds + "秒")
            }
            if(minutes <= 0 && seconds < 10){
                console.log("运行了：" + "0" + seconds + "秒")
            } 
            if(minutes <= 0 && seconds > 10){
                console.log("运行了："  + seconds + "秒")
            } 
        });
    
    }
    //悬浮窗结束-----------